package com.zhouzi.zhaoyang.util

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.tencent.wxpayface.IWxPayfaceCallback
import com.tencent.wxpayface.WxPayFace
import com.zhouzi.zhaoyang.mvp.p.CashActivityPresenter

class FaceUtil {

    fun face(mContext: Context, deviceNo: String, p: CashActivityPresenter) {

        /*初始化刷脸*/
        WxPayFace.getInstance().initWxpayface(mContext, object : IWxPayfaceCallback() {
            override fun response(p0: MutableMap<Any?, Any?>?) {
                Handler(Looper.getMainLooper()).post {
                    if (isSuccessInfo(p0)) {
                        getRawData(mContext, deviceNo, p)
                    } else {
                        Toast.makeText(mContext, "初始化失败，请稍后再试", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })


    }


    private fun getRawData(mContext: Context, deviceNo: String, p: CashActivityPresenter) {
        /*获取rawData*/
        WxPayFace.getInstance().getWxpayfaceRawdata(object : IWxPayfaceCallback() {
            override fun response(p0: MutableMap<Any?, Any?>?) {
                Handler(Looper.getMainLooper()).post {
                    if (isSuccessInfo(p0)) {
                        val rawData = p0?.get("rawdata").toString()
                        val map = mutableMapOf(
                            Pair("deviceNo", deviceNo),
                            Pair("rawdata", rawData)
                        )
                        p.getAuthCode(map)

                    } else {
                        Toast.makeText(mContext, "获取rawData失败，请稍后再试", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })
    }


    /*判断回调是否异常*/
    public fun isSuccessInfo(info: Map<*, *>?): Boolean {

        if (info == null) {
            RuntimeException("调用返回为空").printStackTrace()
            return false
        }
        val code = info["return_code"] as String
        val msg = info["return_msg"] as String
        Log.d("isSuccessInfo", "response | getWxpayfaceRawdata $code | $msg")
        /* info.get("rawdata").toString() == null || code == null ||*/
        if (!code.equals("SUCCESS")) {
            RuntimeException("调用返回非成功信息,return_msg:" + msg + "   ").printStackTrace()
            return false
        }
        return true
    }

}